package com.yt.fras.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.fras.mapper.GroupUserMapper;
import com.yt.fras.pojo.entity.GroupUser;
import com.yt.fras.service.GroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 组成员表 服务实现类
 * </p>
 *
 * @author yt
 * @since 2021-10-25
 */
@Service
@CacheConfig(cacheNames = "groupUserCache")
public class GroupUserServiceImpl extends ServiceImpl<GroupUserMapper, GroupUser> implements GroupUserService {

    @Autowired
    private GroupUserMapper groupUserMapper;

    @Override
    public boolean deleteGroupUserByGroupId(String groupId) {
        QueryWrapper<GroupUser> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);

        List<GroupUser> groupUsers = groupUserMapper.selectList(wrapper);
        if (groupUsers.size() > 0) {
            List<String> groupUsersId = new ArrayList<>();
            for (GroupUser groupUser : groupUsers) {
                groupUsersId.add(groupUser.getId());
            }

            return groupUserMapper.deleteBatchIds(groupUsersId) > 0 ? true : false;
        }
        return true;
    }

    @Override
    public boolean deleteUser(String groupId, String userId) {
        QueryWrapper<GroupUser> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId)
                .eq("user_id", userId);

        GroupUser groupUser = groupUserMapper.selectOne(wrapper);

        return groupUserMapper.deleteById(groupUser.getId()) > 0 ? true : false;
    }

}
